package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.vo.WxCouponVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者：whl
 * 功能：
 */
@Service
public class WxCouponServiceImpl implements WxCouponService {
    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public WxCouponVo list(Integer page, Integer size) {
        PageHelper.startPage(page,size);

        CouponExample couponExample = new CouponExample();
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);//data

        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long count = couponPageInfo.getTotal();//count

        WxCouponVo wxCouponVo = new WxCouponVo();
        wxCouponVo.setData(coupons);
        wxCouponVo.setCount(count);

        return wxCouponVo;
    }

    @Override
    public WxCouponVo mylist(Short status, Integer page, Integer size) {
        PageHelper.startPage(page,size);

        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        Date date = new Date();
        criteria.andStartTimeEqualTo(date);//starttime
        criteria.andEndTimeEqualTo(date);//endtime

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long count = couponPageInfo.getTotal();

        //返回vo
        WxCouponVo wxCouponVo = new WxCouponVo();
        wxCouponVo.setCount(count);
        wxCouponVo.setData(coupons);

        return wxCouponVo;
    }

    @Override
    public List<Coupon> selectList(int userId, Integer grouponRulesId) {
        //获取用户的所有优惠券
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andStatusEqualTo((short) 0);//优惠券应该是status=0还未使用的
        criteria.andDeletedEqualTo(false);//应该是没有删除的
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        //获取对应商品的所有优惠券
        //遍历一遍用户持有的优惠券，因为不支持指定商品，类目，所有应该是通用的才展示，即goodType=0
        ArrayList<Coupon> couponlist = new ArrayList<>();
        for (CouponUser couponUser : couponUsers) {
            //goodsType是couponMapper中才有的属性
            Coupon coupon = couponMapper.selectByPrimaryKey(couponUser.getCouponId());
            if (coupon.getGoodsType() == 0){
                couponlist.add(coupon);
            }
        }

        return couponlist;
    }

    @Override
    public int receive(int userId, Integer couponId) {
        //获取该用户持有的全部优惠券
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        for (CouponUser couponUser : couponUsers) {
            //如果已经领取
            if (couponUser.getCouponId() == couponId){
                //看limit参数：如果是0，无限制；如果是1，限制领取 领取失败【在coupon的表中】
                Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
                if (coupon.getLimit() == 1){
                    return 740;
                }
            }

        }

        //添加到用户优惠券的表中
        Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setDeleted(false);
        couponUser.setCouponId(couponId);
        couponUser.setAddTime(new Date());
        couponUser.setUpdateTime(new Date());
        couponUser.setStatus((short) 0);//使用状态为未使用

        couponUserMapper.insertSelective(couponUser);
        return 0;
    }

    @Override
    public int exchange(String code) {
        //假设userId=1
        int userId = 1;
        //根据优惠码查询该优惠券
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andCodeEqualTo(code);

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        //健壮性
        if (coupons == null || coupons.isEmpty()){
            return 742;
        }
        //将该优惠券插入表中
        int i = receive(userId, coupons.get(0).getId());

        return i;

    }
}
